//
//  YZMRedPacketViewController.m
//  YZMicroStock
//
//  Created by apple on 17/5/2.
//  Copyright © 2017年 cqjr. All rights reserved.
//

#import "YZMRedPacketViewController.h"
#import "YZMRedPacketCell.h"
#import "YZMRedPacketNoCell.h"
#import "YZClient.h"
#import "NSNumber+Utils.h"
#import "YZMRedPacketInfor.h"
#import "YZMRedPacketListModel.h"

static NSString *const sRedPacketCell = @"RedPacketCell";
static NSString *const sRedPacketNoCell = @"RedPacketNoCell";
@interface YZMRedPacketViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UILabel *receiveMoneyLB;
@property (nonatomic, strong) UILabel *balanceLB;
@property (nonatomic, strong) NSMutableArray *datasource;

@end

@implementation YZMRedPacketViewController

-(NSMutableArray *)datasource{
    
    if (!_datasource) {
        
        _datasource = [NSMutableArray array];
    }
    
    return _datasource;
}

- (instancetype)init {
    
    if (self = [super init]) {
        
        self.hidesBottomBarWhenPushed = YES;
        self.title = @"我的红包";
    }
    
    return self;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [YZMAppStyle appBackgroundColor];
    [self CreateUiAndNetWorking];
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:YES];
    [self fetchActivityRedPacketInfor];
    [self fetchRedPacketFundDetail];
}

#pragma mark allway
-(void)CreateUiAndNetWorking{
    
    [self createObjects];
    [self fetchActivityRedPacketInfor];
    [self fetchRedPacketFundDetail];
}


#pragma mark CreateObject
- (void)createObjects {
    
    WS(ws)
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerClass:[YZMRedPacketCell class] forCellReuseIdentifier:sRedPacketCell];
    [self.tableView registerClass:[YZMRedPacketNoCell class] forCellReuseIdentifier:sRedPacketNoCell];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(ws.view);
    }];
    
    //红包头部
    [self createHeader];
}


#pragma mark datasource delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.datasource.count>0) {
        return self.datasource.count;
    }else{
        
    return 1;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.datasource.count>0) {
        
        return 70;
    }else{
        
    return ScreenHeight - 185;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.datasource.count>0) {
        YZMRedPacketCell *cell = [tableView dequeueReusableCellWithIdentifier:sRedPacketCell forIndexPath:indexPath];
        
        YZMIndexCount indexCount;
        indexCount.index =indexPath.row;
        indexCount.count = self.datasource.count;
        cell.indexCount = indexCount;
        YZMRedPacketListModel *model = self.datasource[indexPath.row];
        cell.model = model;
        
        return cell;
    }else{
        
        YZMRedPacketNoCell *cell = [tableView dequeueReusableCellWithIdentifier:sRedPacketNoCell forIndexPath:indexPath];
        return cell;
    }
}

#pragma mark -- createHeaderView
- (void)createHeader {
    
    WS(ws)
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 134)];
    bgView.backgroundColor = [YZMAppStyle appBackgroundColor];
    
    UIView *headerView = [UIView new];
    headerView.backgroundColor = [YZMAppStyle colorForMain];
    [bgView addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(bgView).offset(14);
        make.left.right.equalTo(bgView);
        make.height.mas_equalTo(106);
    }];
    
    UIView *firstView = [[UIView alloc] init];
    [headerView addSubview:firstView];
    [firstView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.bottom.equalTo(headerView);
        make.width.mas_equalTo(ScreenWidth / 2);
    }];
    
    UILabel *receiveTitleLB = [YZMAppStyle labelWithMediumWhite];
    receiveTitleLB.text = @"我收到的红包";
    [firstView addSubview:receiveTitleLB];
    [receiveTitleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(firstView).offset(33);
        make.centerX.equalTo(firstView);
    }];
    
    self.receiveMoneyLB = [YZMAppStyle labelWithFont:[YZMAppStyle smallSizeFont] andColor:[YZMAppStyle colorForWhite]];
    self.receiveMoneyLB.textAlignment = NSTextAlignmentCenter;
    [firstView addSubview:self.receiveMoneyLB];
    
    NSString *receiveMoneyString = @"0.00元";
    NSMutableAttributedString *receiveMoneyStr = [[NSMutableAttributedString alloc]initWithString:receiveMoneyString];
    [receiveMoneyStr addAttribute:NSFontAttributeName value:[YZMAppStyle fontWithSize:24] range:NSMakeRange(0, receiveMoneyString.length-1)];
        self.receiveMoneyLB.attributedText = receiveMoneyStr;
    
    [self.receiveMoneyLB mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(receiveTitleLB.mas_bottom).offset(8);
        make.centerX.equalTo(firstView);
    }];
    
    UIView *secondView = [[UIView alloc] init];
    [headerView addSubview:secondView];
    [secondView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(firstView.mas_right);
        make.top.bottom.right.equalTo(headerView);
    }];
    
    UILabel *balanceTitleLB = [YZMAppStyle labelWithMediumWhite];
    balanceTitleLB.text = @"红包余额";
    [secondView addSubview:balanceTitleLB];
    [balanceTitleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(receiveTitleLB);
        make.centerX.equalTo(secondView);
    }];
    
    self.balanceLB = [YZMAppStyle labelWithFont:[YZMAppStyle smallSizeFont] andColor:[YZMAppStyle colorForWhite]];
    self.balanceLB.textAlignment = NSTextAlignmentCenter;
    [secondView addSubview:self.balanceLB];

    NSMutableAttributedString *balanceStr = [[NSMutableAttributedString alloc]initWithString:receiveMoneyString];
    [balanceStr addAttribute:NSFontAttributeName value:[YZMAppStyle fontWithSize:24] range:NSMakeRange(0, receiveMoneyString.length-1)];
    self.balanceLB.attributedText = balanceStr;

    [self.balanceLB mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(ws.receiveMoneyLB);
        make.centerX.equalTo(secondView);
    }];
    
    UIView *lineView = [YZMAppStyle splitLine];
    [headerView addSubview:lineView];
    [lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.center.equalTo(headerView);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(44);
    }];
    
    self.tableView.tableHeaderView = bgView;
}


#pragma mark netWorking
-(void)fetchActivityRedPacketInfor{
    
    WS(ws)
    [YZClient Activity_RedpacketInforsuccess:^(NSURLSessionDataTask *task, id responseObject) {
        YZGeneralResultModel *topModel = responseObject;
        if (topModel.success) {
            
            YZMRedPacketInfor *model = topModel.data;
            
            NSString *receiveMoneyStr = [NSString stringWithFormat:@"%@元", @(model.tipAllBalance).p02fString];
            ws.receiveMoneyLB.attributedText = [model showAttString:receiveMoneyStr];

            NSString *balanceStr = [NSString stringWithFormat:@"%@元", @(model.tipBalance).p02fString];
            ws.balanceLB.attributedText = [model showAttString:balanceStr];
            
        }else{
            
            [topModel showMessage];
        }
    } failure:^(NSError *error) {
        
        [error yz_showMessage];
    }];
}

- (void)fetchRedPacketFundDetail {
    
    WS(ws)
    [YZClient Activity_fetchRedPacketFundDetailsuccess:^(NSURLSessionDataTask *task, id responseObject) {
        
        YZGeneralResultModel *topModel = responseObject;
        if (topModel.success) {
            
            YZMRedPacketFundDetailModel *model = topModel.data;
            [ws.datasource removeAllObjects];
            [ws.datasource addObjectsFromArray:model.items];
            if (ws.datasource>0) {
                
                [ws.tableView reloadData];
            }
        }else{
            
            [topModel showMessage];
        }
        
    } failure:^(NSError *error) {
        
        [error yz_showMessage];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
